<template>
	<cl-crud ref="Crud">
		<el-row>
			<!-- 刷新按钮 -->
			<cl-refresh-btn />
			<!-- 新增按钮 -->
			<cl-add-btn />
			<!-- 删除按钮 -->
			<cl-multi-delete-btn />
			<cl-flex1 />
			<!-- 关键字搜索 -->
			<cl-search-key />
		</el-row>

		<el-row>
			<!-- 数据表格 -->
			<cl-table ref="Table" />
		</el-row>

		<el-row>
			<cl-flex1 />
			<!-- 分页控件 -->
			<cl-pagination />
		</el-row>

		<!-- 新增、编辑 -->
		<cl-upsert ref="Upsert" />
	</cl-crud>
</template>

<script lang="ts" name="app-user" setup>
import { useCrud, useTable, useUpsert } from "@cool-vue/crud";
import { next } from "dom7";
import { json } from "stream/consumers";
import { useCool } from "/@/cool";

const { service } = useCool();

// cl-upsert 配置
const Upsert = useUpsert({
	items: [
		{ label: "头像", prop: "avatar", component: { name: "cl-upload" }, required: true },
		{ label: "昵称", prop: "nickname", span: 12, required: true, component: { name: "el-input" } },
		{
			label: "姓名",
			prop: "name",
			span: 12,
			component: {
				name: "el-input",
				props: {
					disabled: true
				}
			}
		},
		{
			label: "性别",
			prop: "sex",
			span: 12,
			required: true,
			component: {
				name: "el-select",
				options: [
					{ label: "男", value: 1 },
					{ label: "女", value: 2 },
					{ label: "人妖", value: 0 }
				]
			}
		},
		{
			label: "手机",
			prop: "mobile",
			span: 12,
			component: {
				name: "el-input",
				props: {
					disabled: true
				}
			}
		},
		{
			label: "学校",
			prop: "school",
			span: 12,
			component: {
				name: "el-input",
				props: {
					disabled: true
				}
			}
		},
		{
			label: "学院",
			prop: "college",
			span: 12,
			component: {
				name: "el-input",
				props: {
					disabled: true
				}
			}
		},
		{
			label: "班级",
			prop: "class",
			span: 12,
			component: {
				name: "el-input",
				props: {
					disabled: true
				}
			}
		},
		{
			label: "UID",
			prop: "openid",
			span: 12,
			component: {
				name: "el-input",
				props: {
					disabled: true
				}
			}
		},
		{ label: "学号", prop: "username", span: 12, component: { name: "el-input" } },
		{
			label: "会员",
			prop: "expire",
			span: 12,
			component: {
				name: "el-date-picker",
				props: {
					type: "datetime"
				}
			}
		},
		{
			label: "密码",
			prop: "password",
			span: 12,
			component: {
				name: "el-input",
				props: {
					type: "password",
					showPassword: true
				}
			}
		},
		{
			label: "状态",
			prop: "status",
			span: 12,
			value: 1,
			component: {
				name: "el-select",
				options: [
					{ label: "禁用", value: 0 },
					{ label: "正常", value: 1 }
				]
			},
			required: true
		}
	],
	// 保存前处理
	async onInfo(data, { done, next }) {
		const { name } = data.schoolId ? await service.app.school.info({ id: data.schoolId }) : { name: "" };
		let newData = await next({
			...data
		});
		newData.school = name;
		done(newData);
	}
});

// cl-table 配置
const Table = useTable({
	columns: [
		{ type: "selection" },
		{ label: "ID", prop: "id", width: 60 },
		{ label: "头像", prop: "avatar", width: 100, component: { name: "cl-image", props: { size: 60 } } },
		{ label: "姓名", prop: "name", width: 70 },
		{
			label: "性别",
			prop: "sex",
			width: 75,
			dict: [
				{ label: "男", value: 1, type: "success" },
				{ label: "女", value: 2, type: "danger" },
				{ label: "人妖", value: 0, type: "warning" }
			]
		},
		{ label: "手机", prop: "mobile", width: 120 },
		{ label: "学校", prop: "schoolName" },
		{ label: "学院", prop: "college" },
		{ label: "班级", prop: "class", width: 180 },
		{ label: "学号", prop: "username" },
		{
			label: "提醒",
			prop: "remind",
			component: {
				name: "cl-switch",
			}
		},
		{
			label: "状态",
			prop: "status",
			dict: [
				{ label: "禁用", value: 0, type: "danger" },
				{ label: "正常", value: 1, type: "success" }
			]
		},
		{ type: "op", buttons: ["edit", "delete"] }
	]
});

// cl-crud 配置
const Crud = useCrud(
	{
		service: service.app.user
	},
	(app) => {
		app.refresh();
	}
);
</script>